package com.cencat.order.controller;

import com.cencat.common.annotation.Log;
import com.cencat.common.core.controller.BaseController;
import com.cencat.common.core.domain.AjaxResult;
import com.cencat.common.core.page.TableDataInfo;
import com.cencat.common.enums.BusinessType;
import com.cencat.common.utils.poi.ExcelUtil;
import com.cencat.order.domain.RouteInvoice;
import com.cencat.order.domain.InvoiceItem;
import com.cencat.order.domain.InvoiceTemplate;
import com.cencat.order.domain.vo.RouteInvoiceVO;
import com.cencat.order.domain.vo.InvoiceStatisticsVO;
import com.cencat.order.domain.vo.InvoiceAnalysisVO;
import com.cencat.order.domain.dto.RouteInvoiceCreateDTO;
import com.cencat.order.domain.dto.RouteInvoiceUpdateDTO;
import com.cencat.order.domain.dto.InvoiceTemplateDTO;
import com.cencat.order.domain.dto.InvoiceBatchDTO;
import com.cencat.order.service.IRouteInvoiceService;
import com.cencat.order.service.IInvoiceItemService;
import com.cencat.order.service.IInvoiceTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 线路发票管理系统控制器
 * 
 * @author cencat
 * @date 2024-01-20
 */
@Tag(name = "线路发票管理", description = "线路发票管理系统核心功能接口")
@RestController
@RequestMapping("/route-invoice")
@RequiredArgsConstructor
@Validated
public class RouteInvoiceController extends BaseController {

    private final IRouteInvoiceService routeInvoiceService;
    private final IInvoiceItemService invoiceItemService;
    private final IInvoiceTemplateService invoiceTemplateService;

    /**
     * 查询发票列表
     */
    @Operation(summary = "查询发票列表", description = "分页查询线路发票信息")
    @PreAuthorize("@ss.hasPermi('invoice:list')")
    @GetMapping("/list")
    public TableDataInfo list(RouteInvoice routeInvoice) {
        startPage();
        List<RouteInvoice> list = routeInvoiceService.selectInvoiceList(routeInvoice);
        return getDataTable(list);
    }

    /**
     * 获取发票详情
     */
    @Operation(summary = "获取发票详情", description = "根据发票ID获取详细信息")
    @PreAuthorize("@ss.hasPermi('invoice:query')")
    @GetMapping(value = "/{invoiceId}")
    public AjaxResult getInfo(@Parameter(description = "发票ID") @PathVariable Long invoiceId) {
        RouteInvoiceVO invoiceVO = routeInvoiceService.getInvoiceDetail(invoiceId);
        return success(invoiceVO);
    }

    /**
     * 新增发票
     */
    @Operation(summary = "新增发票", description = "创建新的线路发票")
    @PreAuthorize("@ss.hasPermi('invoice:add')")
    @Log(title = "发票管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody RouteInvoiceCreateDTO createDTO) {
        RouteInvoiceVO invoiceVO = routeInvoiceService.createInvoice(createDTO);
        return success(invoiceVO);
    }

    /**
     * 修改发票
     */
    @Operation(summary = "修改发票", description = "更新发票信息")
    @PreAuthorize("@ss.hasPermi('invoice:edit')")
    @Log(title = "发票管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody RouteInvoiceUpdateDTO updateDTO) {
        boolean result = routeInvoiceService.updateInvoice(updateDTO);
        return toAjax(result);
    }

    /**
     * 删除发票
     */
    @Operation(summary = "删除发票", description = "删除指定发票")
    @PreAuthorize("@ss.hasPermi('invoice:remove')")
    @Log(title = "发票管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{invoiceIds}")
    public AjaxResult remove(@Parameter(description = "发票ID数组") @PathVariable Long[] invoiceIds) {
        boolean result = routeInvoiceService.deleteInvoiceByIds(invoiceIds);
        return toAjax(result);
    }

    /**
     * 根据路线生成发票
     */
    @Operation(summary = "根据路线生成发票", description = "基于路线配送数据自动生成发票")
    @PreAuthorize("@ss.hasPermi('invoice:generate')")
    @Log(title = "生成发票", businessType = BusinessType.INSERT)
    @PostMapping("/generate-by-route")
    public AjaxResult generateByRoute(@RequestParam Long routeId,
                                     @RequestParam String startDate,
                                     @RequestParam String endDate,
                                     @RequestParam(required = false) String invoiceType,
                                     @RequestParam(required = false) Long templateId) {
        RouteInvoiceVO invoiceVO = routeInvoiceService.generateByRoute(routeId, startDate, endDate, invoiceType, templateId);
        return success(invoiceVO);
    }

    /**
     * 根据商家生成发票
     */
    @Operation(summary = "根据商家生成发票", description = "基于商家配送数据自动生成发票")
    @PreAuthorize("@ss.hasPermi('invoice:generate')")
    @Log(title = "生成发票", businessType = BusinessType.INSERT)
    @PostMapping("/generate-by-merchant")
    public AjaxResult generateByMerchant(@RequestParam Long merchantId,
                                        @RequestParam String startDate,
                                        @RequestParam String endDate,
                                        @RequestParam(required = false) String invoiceType,
                                        @RequestParam(required = false) Long templateId) {
        RouteInvoiceVO invoiceVO = routeInvoiceService.generateByMerchant(merchantId, startDate, endDate, invoiceType, templateId);
        return success(invoiceVO);
    }

    /**
     * 批量生成发票
     */
    @Operation(summary = "批量生成发票", description = "批量生成多个发票")
    @PreAuthorize("@ss.hasPermi('invoice:batch:generate')")
    @Log(title = "批量生成发票", businessType = BusinessType.INSERT)
    @PostMapping("/batch-generate")
    public AjaxResult batchGenerate(@Valid @RequestBody InvoiceBatchDTO batchDTO) {
        List<RouteInvoiceVO> invoices = routeInvoiceService.batchGenerate(batchDTO);
        return success(invoices);
    }

    /**
     * 发票审核
     */
    @Operation(summary = "发票审核", description = "审核发票信息")
    @PreAuthorize("@ss.hasPermi('invoice:audit')")
    @Log(title = "发票审核", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult auditInvoice(@RequestParam Long invoiceId,
                                  @RequestParam String auditStatus,
                                  @RequestParam(required = false) String auditRemark) {
        boolean result = routeInvoiceService.auditInvoice(invoiceId, auditStatus, auditRemark);
        return toAjax(result);
    }

    /**
     * 批量审核发票
     */
    @Operation(summary = "批量审核发票", description = "批量审核多个发票")
    @PreAuthorize("@ss.hasPermi('invoice:batch:audit')")
    @Log(title = "批量审核", businessType = BusinessType.UPDATE)
    @PostMapping("/batch-audit")
    public AjaxResult batchAudit(@RequestParam Long[] invoiceIds,
                                @RequestParam String auditStatus,
                                @RequestParam(required = false) String auditRemark) {
        boolean result = routeInvoiceService.batchAudit(invoiceIds, auditStatus, auditRemark);
        return toAjax(result);
    }

    /**
     * 发票打印
     */
    @Operation(summary = "发票打印", description = "打印发票")
    @PreAuthorize("@ss.hasPermi('invoice:print')")
    @Log(title = "发票打印", businessType = BusinessType.OTHER)
    @PostMapping("/print/{invoiceId}")
    public AjaxResult printInvoice(@Parameter(description = "发票ID") @PathVariable Long invoiceId,
                                  @RequestParam(required = false) String printType) {
        Map<String, Object> printData = routeInvoiceService.printInvoice(invoiceId, printType);
        return success(printData);
    }

    /**
     * 批量打印发票
     */
    @Operation(summary = "批量打印发票", description = "批量打印多个发票")
    @PreAuthorize("@ss.hasPermi('invoice:batch:print')")
    @Log(title = "批量打印", businessType = BusinessType.OTHER)
    @PostMapping("/batch-print")
    public AjaxResult batchPrint(@RequestParam Long[] invoiceIds,
                                @RequestParam(required = false) String printType) {
        Map<String, Object> printData = routeInvoiceService.batchPrint(invoiceIds, printType);
        return success(printData);
    }

    /**
     * 发票预览
     */
    @Operation(summary = "发票预览", description = "预览发票内容")
    @PreAuthorize("@ss.hasPermi('invoice:preview')")
    @GetMapping("/preview/{invoiceId}")
    public AjaxResult previewInvoice(@Parameter(description = "发票ID") @PathVariable Long invoiceId,
                                    @RequestParam(required = false) String previewType) {
        Map<String, Object> previewData = routeInvoiceService.previewInvoice(invoiceId, previewType);
        return success(previewData);
    }

    /**
     * 发票状态跟踪
     */
    @Operation(summary = "发票状态跟踪", description = "跟踪发票状态变更")
    @PreAuthorize("@ss.hasPermi('invoice:track')")
    @GetMapping("/track/{invoiceId}")
    public AjaxResult trackInvoice(@Parameter(description = "发票ID") @PathVariable Long invoiceId) {
        List<Map<String, Object>> trackHistory = routeInvoiceService.trackInvoice(invoiceId);
        return success(trackHistory);
    }

    /**
     * 更新发票状态
     */
    @Operation(summary = "更新发票状态", description = "更新发票的处理状态")
    @PreAuthorize("@ss.hasPermi('invoice:status')")
    @Log(title = "发票状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatus(@RequestParam Long invoiceId,
                                  @RequestParam String status,
                                  @RequestParam(required = false) String remark) {
        boolean result = routeInvoiceService.updateInvoiceStatus(invoiceId, status, remark);
        return toAjax(result);
    }

    /**
     * 发票明细管理
     */
    @Operation(summary = "查询发票明细", description = "查询发票的明细项目")
    @PreAuthorize("@ss.hasPermi('invoice:item:list')")
    @GetMapping("/items/{invoiceId}")
    public TableDataInfo getInvoiceItems(@Parameter(description = "发票ID") @PathVariable Long invoiceId) {
        startPage();
        List<InvoiceItem> list = invoiceItemService.getInvoiceItems(invoiceId);
        return getDataTable(list);
    }

    /**
     * 新增发票明细
     */
    @Operation(summary = "新增发票明细", description = "为发票添加明细项目")
    @PreAuthorize("@ss.hasPermi('invoice:item:add')")
    @Log(title = "发票明细", businessType = BusinessType.INSERT)
    @PostMapping("/item")
    public AjaxResult addInvoiceItem(@RequestParam Long invoiceId,
                                    @RequestParam String itemName,
                                    @RequestParam String itemType,
                                    @RequestParam Integer quantity,
                                    @RequestParam Double unitPrice,
                                    @RequestParam(required = false) String remark) {
        InvoiceItem item = invoiceItemService.addInvoiceItem(invoiceId, itemName, itemType, quantity, unitPrice, remark);
        return success("添加成功", item.getId());
    }

    /**
     * 修改发票明细
     */
    @Operation(summary = "修改发票明细", description = "修改发票明细项目")
    @PreAuthorize("@ss.hasPermi('invoice:item:edit')")
    @Log(title = "发票明细", businessType = BusinessType.UPDATE)
    @PutMapping("/item")
    public AjaxResult editInvoiceItem(@RequestParam Long itemId,
                                     @RequestParam(required = false) String itemName,
                                     @RequestParam(required = false) Integer quantity,
                                     @RequestParam(required = false) Double unitPrice,
                                     @RequestParam(required = false) String remark) {
        boolean result = invoiceItemService.editInvoiceItem(itemId, itemName, quantity, unitPrice, remark);
        return toAjax(result);
    }

    /**
     * 删除发票明细
     */
    @Operation(summary = "删除发票明细", description = "删除发票明细项目")
    @PreAuthorize("@ss.hasPermi('invoice:item:remove')")
    @Log(title = "发票明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/item/{itemIds}")
    public AjaxResult removeInvoiceItem(@Parameter(description = "明细ID数组") @PathVariable Long[] itemIds) {
        boolean result = invoiceItemService.deleteInvoiceItemByIds(itemIds);
        return toAjax(result);
    }

    /**
     * 发票模板管理
     */
    @Operation(summary = "查询发票模板", description = "查询发票模板列表")
    @PreAuthorize("@ss.hasPermi('invoice:template:list')")
    @GetMapping("/templates")
    public TableDataInfo getTemplates(InvoiceTemplate invoiceTemplate) {
        startPage();
        List<InvoiceTemplate> list = invoiceTemplateService.selectTemplateList(invoiceTemplate);
        return getDataTable(list);
    }

    /**
     * 新增发票模板
     */
    @Operation(summary = "新增发票模板", description = "创建新的发票模板")
    @PreAuthorize("@ss.hasPermi('invoice:template:add')")
    @Log(title = "发票模板", businessType = BusinessType.INSERT)
    @PostMapping("/template")
    public AjaxResult addTemplate(@Valid @RequestBody InvoiceTemplateDTO templateDTO) {
        InvoiceTemplate template = invoiceTemplateService.createTemplate(templateDTO);
        return success("创建成功", template.getId());
    }

    /**
     * 修改发票模板
     */
    @Operation(summary = "修改发票模板", description = "更新发票模板")
    @PreAuthorize("@ss.hasPermi('invoice:template:edit')")
    @Log(title = "发票模板", businessType = BusinessType.UPDATE)
    @PutMapping("/template")
    public AjaxResult editTemplate(@Valid @RequestBody InvoiceTemplateDTO templateDTO) {
        boolean result = invoiceTemplateService.updateTemplate(templateDTO);
        return toAjax(result);
    }

    /**
     * 删除发票模板
     */
    @Operation(summary = "删除发票模板", description = "删除发票模板")
    @PreAuthorize("@ss.hasPermi('invoice:template:remove')")
    @Log(title = "发票模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/template/{templateIds}")
    public AjaxResult removeTemplate(@Parameter(description = "模板ID数组") @PathVariable Long[] templateIds) {
        boolean result = invoiceTemplateService.deleteTemplateByIds(templateIds);
        return toAjax(result);
    }

    /**
     * 启用/禁用发票模板
     */
    @Operation(summary = "启用禁用模板", description = "启用或禁用发票模板")
    @PreAuthorize("@ss.hasPermi('invoice:template:status')")
    @Log(title = "发票模板", businessType = BusinessType.UPDATE)
    @PutMapping("/template/status")
    public AjaxResult changeTemplateStatus(@RequestParam Long templateId,
                                          @RequestParam String status) {
        boolean result = invoiceTemplateService.updateTemplateStatus(templateId, status);
        return toAjax(result);
    }

    /**
     * 复制发票模板
     */
    @Operation(summary = "复制发票模板", description = "复制现有发票模板")
    @PreAuthorize("@ss.hasPermi('invoice:template:copy')")
    @Log(title = "发票模板", businessType = BusinessType.INSERT)
    @PostMapping("/template/copy")
    public AjaxResult copyTemplate(@RequestParam Long templateId,
                                  @RequestParam String newTemplateName) {
        InvoiceTemplate template = invoiceTemplateService.copyTemplate(templateId, newTemplateName);
        return success("复制成功", template.getId());
    }

    /**
     * 发票统计信息
     */
    @Operation(summary = "发票统计", description = "获取发票统计数据")
    @PreAuthorize("@ss.hasPermi('invoice:statistics')")
    @GetMapping("/statistics")
    public AjaxResult getStatistics(@RequestParam(required = false) String startDate,
                                   @RequestParam(required = false) String endDate,
                                   @RequestParam(required = false) String dimension,
                                   @RequestParam(required = false) String invoiceType) {
        InvoiceStatisticsVO statistics = routeInvoiceService.getInvoiceStatistics(startDate, endDate, dimension, invoiceType);
        return success(statistics);
    }

    /**
     * 发票金额分析
     */
    @Operation(summary = "金额分析", description = "分析发票金额趋势")
    @PreAuthorize("@ss.hasPermi('invoice:analyze')")
    @GetMapping("/amount-analysis")
    public AjaxResult getAmountAnalysis(@RequestParam(required = false) String startDate,
                                       @RequestParam(required = false) String endDate,
                                       @RequestParam(required = false) String groupBy,
                                       @RequestParam(required = false) String analysisType) {
        InvoiceAnalysisVO analysis = routeInvoiceService.getAmountAnalysis(startDate, endDate, groupBy, analysisType);
        return success(analysis);
    }

    /**
     * 发票效率分析
     */
    @Operation(summary = "效率分析", description = "分析发票处理效率")
    @PreAuthorize("@ss.hasPermi('invoice:efficiency')")
    @GetMapping("/efficiency-analysis")
    public AjaxResult getEfficiencyAnalysis(@RequestParam(required = false) String startDate,
                                           @RequestParam(required = false) String endDate,
                                           @RequestParam(required = false) String metricType) {
        Map<String, Object> analysis = routeInvoiceService.getEfficiencyAnalysis(startDate, endDate, metricType);
        return success(analysis);
    }

    /**
     * 发票对账
     */
    @Operation(summary = "发票对账", description = "进行发票对账处理")
    @PreAuthorize("@ss.hasPermi('invoice:reconcile')")
    @Log(title = "发票对账", businessType = BusinessType.UPDATE)
    @PostMapping("/reconcile")
    public AjaxResult reconcileInvoice(@RequestParam Long invoiceId,
                                      @RequestParam String reconcileType,
                                      @RequestParam(required = false) String reconcileData) {
        Map<String, Object> result = routeInvoiceService.reconcileInvoice(invoiceId, reconcileType, reconcileData);
        return success(result);
    }

    /**
     * 批量对账
     */
    @Operation(summary = "批量对账", description = "批量进行发票对账")
    @PreAuthorize("@ss.hasPermi('invoice:batch:reconcile')")
    @Log(title = "批量对账", businessType = BusinessType.UPDATE)
    @PostMapping("/batch-reconcile")
    public AjaxResult batchReconcile(@RequestParam Long[] invoiceIds,
                                    @RequestParam String reconcileType) {
        Map<String, Object> result = routeInvoiceService.batchReconcile(invoiceIds, reconcileType);
        return success(result);
    }

    /**
     * 发票归档
     */
    @Operation(summary = "发票归档", description = "归档已完成的发票")
    @PreAuthorize("@ss.hasPermi('invoice:archive')")
    @Log(title = "发票归档", businessType = BusinessType.UPDATE)
    @PostMapping("/archive")
    public AjaxResult archiveInvoice(@RequestParam Long invoiceId,
                                    @RequestParam(required = false) String archiveRemark) {
        boolean result = routeInvoiceService.archiveInvoice(invoiceId, archiveRemark);
        return toAjax(result);
    }

    /**
     * 批量归档
     */
    @Operation(summary = "批量归档", description = "批量归档发票")
    @PreAuthorize("@ss.hasPermi('invoice:batch:archive')")
    @Log(title = "批量归档", businessType = BusinessType.UPDATE)
    @PostMapping("/batch-archive")
    public AjaxResult batchArchive(@RequestParam Long[] invoiceIds,
                                  @RequestParam(required = false) String archiveRemark) {
        boolean result = routeInvoiceService.batchArchive(invoiceIds, archiveRemark);
        return toAjax(result);
    }

    /**
     * 发票搜索
     */
    @Operation(summary = "发票搜索", description = "根据条件搜索发票")
    @PreAuthorize("@ss.hasPermi('invoice:search')")
    @GetMapping("/search")
    public TableDataInfo searchInvoice(@RequestParam(required = false) String keyword,
                                      @RequestParam(required = false) String invoiceNo,
                                      @RequestParam(required = false) String merchantName,
                                      @RequestParam(required = false) String startDate,
                                      @RequestParam(required = false) String endDate,
                                      @RequestParam(required = false) String status) {
        startPage();
        List<RouteInvoice> list = routeInvoiceService.searchInvoice(keyword, invoiceNo, merchantName, startDate, endDate, status);
        return getDataTable(list);
    }

    /**
     * 发票验证
     */
    @Operation(summary = "发票验证", description = "验证发票信息的准确性")
    @PreAuthorize("@ss.hasPermi('invoice:validate')")
    @PostMapping("/validate")
    public AjaxResult validateInvoice(@RequestParam Long invoiceId,
                                     @RequestParam(required = false) String validateType) {
        Map<String, Object> result = routeInvoiceService.validateInvoice(invoiceId, validateType);
        return success(result);
    }

    /**
     * 发票重新计算
     */
    @Operation(summary = "重新计算", description = "重新计算发票金额")
    @PreAuthorize("@ss.hasPermi('invoice:recalculate')")
    @Log(title = "发票重算", businessType = BusinessType.UPDATE)
    @PostMapping("/recalculate")
    public AjaxResult recalculateInvoice(@RequestParam Long invoiceId,
                                        @RequestParam(required = false) String recalculateType) {
        RouteInvoiceVO invoiceVO = routeInvoiceService.recalculateInvoice(invoiceId, recalculateType);
        return success(invoiceVO);
    }

    /**
     * 发票数据导出
     */
    @Operation(summary = "导出发票数据", description = "导出发票信息到Excel")
    @PreAuthorize("@ss.hasPermi('invoice:export')")
    @Log(title = "发票管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RouteInvoice routeInvoice) {
        List<RouteInvoice> list = routeInvoiceService.selectInvoiceList(routeInvoice);
        ExcelUtil<RouteInvoice> util = new ExcelUtil<RouteInvoice>(RouteInvoice.class);
        util.exportExcel(response, list, "发票数据");
    }

    /**
     * 发票数据导入
     */
    @Operation(summary = "导入发票数据", description = "从Excel导入发票信息")
    @PreAuthorize("@ss.hasPermi('invoice:import')")
    @Log(title = "发票管理", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RouteInvoice> util = new ExcelUtil<RouteInvoice>(RouteInvoice.class);
        List<RouteInvoice> invoiceList = util.importExcel(file.getInputStream());
        String message = routeInvoiceService.importInvoices(invoiceList);
        return success(message);
    }

    /**
     * 发票报表生成
     */
    @Operation(summary = "生成报表", description = "生成发票相关报表")
    @PreAuthorize("@ss.hasPermi('invoice:report')")
    @PostMapping("/generate-report")
    public AjaxResult generateReport(@RequestParam String reportType,
                                    @RequestParam(required = false) String startDate,
                                    @RequestParam(required = false) String endDate,
                                    @RequestParam(required = false) String reportFormat) {
        Map<String, Object> report = routeInvoiceService.generateReport(reportType, startDate, endDate, reportFormat);
        return success(report);
    }

    /**
     * 发票提醒设置
     */
    @Operation(summary = "提醒设置", description = "设置发票相关提醒")
    @PreAuthorize("@ss.hasPermi('invoice:reminder')")
    @Log(title = "发票提醒", businessType = BusinessType.UPDATE)
    @PostMapping("/set-reminder")
    public AjaxResult setReminder(@RequestParam String reminderType,
                                 @RequestParam String reminderRule,
                                 @RequestParam(required = false) String notifyUsers,
                                 @RequestParam(required = false) String reminderContent) {
        boolean result = routeInvoiceService.setReminder(reminderType, reminderRule, notifyUsers, reminderContent);
        return toAjax(result);
    }

    /**
     * 发票健康检查
     */
    @Operation(summary = "健康检查", description = "检查发票系统健康状态")
    @PreAuthorize("@ss.hasPermi('invoice:health')")
    @GetMapping("/health-check")
    public AjaxResult healthCheck() {
        Map<String, Object> healthStatus = routeInvoiceService.healthCheck();
        return success(healthStatus);
    }

    /**
     * 发票数据同步
     */
    @Operation(summary = "数据同步", description = "同步发票相关数据")
    @PreAuthorize("@ss.hasPermi('invoice:sync')")
    @Log(title = "数据同步", businessType = BusinessType.UPDATE)
    @PostMapping("/sync-data")
    public AjaxResult syncData(@RequestParam(required = false) String syncType,
                              @RequestParam(required = false) String startDate,
                              @RequestParam(required = false) String endDate) {
        boolean result = routeInvoiceService.syncData(syncType, startDate, endDate);
        return toAjax(result);
    }

    /**
     * 发票缓存刷新
     */
    @Operation(summary = "缓存刷新", description = "刷新发票系统缓存")
    @PreAuthorize("@ss.hasPermi('invoice:cache:refresh')")
    @Log(title = "缓存刷新", businessType = BusinessType.UPDATE)
    @PostMapping("/refresh-cache")
    public AjaxResult refreshCache(@RequestParam(required = false) String cacheType) {
        boolean result = routeInvoiceService.refreshCache(cacheType);
        return toAjax(result);
    }

    /**
     * 批量更新发票状态
     */
    @Operation(summary = "批量更新状态", description = "批量更新多个发票的状态")
    @PreAuthorize("@ss.hasPermi('invoice:batch:status')")
    @Log(title = "发票管理", businessType = BusinessType.UPDATE)
    @PutMapping("/batch-status")
    public AjaxResult batchUpdateStatus(@RequestParam Long[] invoiceIds,
                                       @RequestParam String status,
                                       @RequestParam(required = false) String remark) {
        boolean result = routeInvoiceService.batchUpdateStatus(invoiceIds, status, remark);
        return toAjax(result);
    }
}